import { SettingsService } from '@delon/theme';
import { Component, Injector, Output, EventEmitter, ElementRef, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { HttpClient, HttpParams } from '@angular/common/http';
import { ChannelGroupEditDto } from "@shared/model/channelgroupdto";
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { AppComponentBase } from "@shared/component-base/app-component-base";

@Component({
  selector: 'edit-channelgroup-modal',
  templateUrl: './edit-channelgroup.component.html'
})

export class EditChannelGroupComponent extends AppComponentBase implements OnInit {
  isVisible = false;
  active: boolean = false;
  validateForm: FormGroup;
  channelGroup: ChannelGroupEditDto = null;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
  constructor(
    private injector: Injector,
    private httpClient: HttpClient,
    private fb: FormBuilder,
    public msg: NzMessageService
  ) {
    super(injector);
  }

  showModal(id: number): void {
    this.channelGroup = new ChannelGroupEditDto();
    let params = new HttpParams().append('id', `${id}`);
    this.httpClient.get("channelgroup/info", {params}).subscribe(
      (val) => {
        const result: any = val;
        if (result && !result.success) {
          this.msg.error(result.message);
          return null;
        } else {
          this.channelGroup.init(result.data);
        }
      });

    this.validateForm.reset();
    this.active = true;
    this.isVisible = true;
  }

  handleSave(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }

    if (!this.validateForm.valid) {
      return;
    }

    this.httpClient.post("channelgroup/edit", this.channelGroup.toJSON()).subscribe(
      (val) => {
        const result: any = val;
        if (result && !result.success) {
          this.msg.error(result.message);
          return null;
        } else {
          this.msg.create("success", "保存成功！");
          this.active = false;
          this.isVisible = false;
          this.modalSave.emit(null);
        }
      });
  }

  handleCancel(): void {
    this.active = false;
    this.isVisible = false;
  }

  updateConfirmValidator(): void {
    /** wait for refresh value */
    Promise.resolve().then(() => this.validateForm.controls.checkPassword.updateValueAndValidity());
  }

  confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.validateForm.controls.password.value) {
      return { confirm: true, error: true };
    }
  };

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      id: [null, [Validators.required]],
      name: [null, [Validators.required]],
      isShow: [null, [Validators.required]],
      orderNo: [null, [Validators.required]],
    });
  }
}


